import create from '../../../utils/create2.js'
import store from '../../../store/index.js'
import PagePromoterUtil, { PagePromoter } from '../../../data/common/pagePromoter';
import AppSceneUtil from '../../../data/common/appScene';
import SessionUtil from "../../../data/session/session"
const Common = require('../../../utils/common.js');
create(store, {
  data: {
    consultantId: 0,
    bookid: '',
    share: false,
    isPJH: false,
    tasting: {},
    detail: false,
    visible: false,
    markers: [],
    defaultImg:  `https://${store.data.requestDomain}/Public/Wap/images/user/userimg_180522.png`
  },
  onLoad: function (options) {
    getApp().getSaleMode()
    // 开启转发按钮
    PagePromoterUtil.showShareMenu(true).then()
    SessionUtil.getInstance().then(session => {
      // 注册当前页面的场景信息
      AppSceneUtil.setPageScene(this)
      const {order, bookid,from,detail,share} = options
      if (from && from === 'PJH') {
        wx.setNavigationBarColor({
          frontColor: '#000000',
          backgroundColor: '#fffff',
        })
        wx.setNavigationBarTitle({
          title: '美酒品鉴会',
        })
        this.setData({
          isPJH: true,
          bookid
        })
        Common.my_request({
          url: '/AppMaster/BookDinner/getBookDinnerOrderInfo',
          data: {
            book_id: bookid
          },
          success: (res) => {
            if (res.data.result == 0) {
              let {
                data
              } = res.data
              data['dr_longitude'] = data.longitude
              data['dr_latitude'] = data.latitude
              this.setData({
                BookInfo: res.data.data,
                markers: [{
                  id: 1,
                  longitude: data.longitude,
                  latitude: data.latitude
                }]
              })
            }
          }
        })
      } else {
        if (order) {
          wx.setNavigationBarTitle({
            title: "订餐信息"
          })
          Common.my_request({
            url: '/AppMaster/BookDinner/getBookDinnerOrderInfo',
            data: {
              book_id: order
            },
            success: (res) => {
              if (res.data.result == 0) {
                let {
                  data
                } = res.data
                data['dr_longitude'] = data.longitude
                data['dr_latitude'] = data.latitude
                this.setData({
                  BookInfo: res.data.data,
                  share: share ? share : false,
                  markers: [{
                    id: 1,
                    longitude: data.longitude,
                    latitude: data.latitude
                  }]
                })
              }
            }
          })
        } else { //创建订餐订单成功
          wx.setNavigationBarTitle({
            title: "订餐成功"
          })
          this.setData({
            share: false,
            bookid,
            consultantId: wx.getStorageSync('consultantId'),
          });
          this.getInfor();
          this.getLastBookInfo();
        }
      }
      if (detail && detail * 1 === 1) {
        this.setData({
          detail: true
        })
      }
    })
  },
  onShow: function(){
    SessionUtil.getInstance().then(()=>{
      // 注册当前页面的场景信息
      AppSceneUtil.setPageScene(this, false)
    })
  },
  Call: function () {
    var mobile = this.data.salesmanVCard.contactPhone.split("-").join('')
    wx.makePhoneCall({
      phoneNumber: mobile
    })
  },
  callFriend(e) {
    const {
      dataset
    } = e.currentTarget
    const {
      phone
    } = dataset
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  /**
   * 获取顾问信息
   */
  getInfor: function () {
    SessionUtil.getInstance().then(session => {
      let server = session.getTheSellingServerSync(), vCard = server.getTheVCardSync()
      this.setData({
        salesmanVCard : {
          avatarUri:vCard.avatarUri,
          showName: vCard.name,
          contactPhone: vCard.contactPhone
        }
      })
    })
  },
  /**
   * 获取最近一次
   */
  getLastBookInfo: function () {
    var page = this
    Common.my_request({
      url: '/AppMaster/BookDinner/getLastBookInfo',
      success: function (res) {
        if (res.data.result == 0) {
          page.setData({
            BookInfo: res.data.data
          })
        }
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.stopPullDownRefresh()
  },
  tiao: function () {
    // this.data.roomId = ''
    // this.data.dinner = {}
    // this.data.room = {}
    // this.update()
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  onShareAppMessage: function () {
    let {BookInfo, isPJH} = this.data
    BookInfo.dr_img_uri = BookInfo.dr_img_uri ? BookInfo.dr_img_uri : BookInfo.dinner_room_img_uri
    const {judge_info } = BookInfo
    let shareTitle =  `您的好友${BookInfo.name}已在${BookInfo.dr_name}订好餐,希望您准时赴宴`,path,query,imageUrl
    if(isPJH){
      path = '/pages/tastingShare/index'
      query = {id:BookInfo.book_id}
      imageUrl = judge_info.show_page_img
    }else{
      path = '/pages/index/foodsuccess/index'
      query = {order:BookInfo.book_id,share:true}
      imageUrl = BookInfo.dr_img_uri
    }
    return (new PagePromoter( path, query))
        .setShareTitle(shareTitle)
        .setShareImage(imageUrl)
        .buildShareInfo()
  },
  /**
   * 加入用餐
   */
  joinDinner() {
    const { BookInfo } = this.data
    Common.my_request({
      url: '/AppMaster/Taste/judgeJoin',
      data: {
        id: BookInfo.judge_info.judge_id,
        salesman_id: BookInfo.judge_info.salesman_id
      },
      success: (res) => {
        const { data, result, message } = res.data
        if (result === 0) {
          this.setData({
            visible: true
          })
        } else {
          Common.Alert('提示', message);
        }
      }
    });
  }
})